package com.ruoyi.service;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.entity.ClearingMerchant;
import com.ruoyi.mapper.ClearingMerchantMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.bean.ClearingMerchantFrom;
import com.sxftech.resp.ResponseData;

import java.util.List;

/**
 * <p>
 * 清分商户列表 服务实现类
 * </p>
 *
 * @author liuxing
 * @since 2020-11-09
 */
@Service
public class ClearingMerchantService extends ServiceImpl<ClearingMerchantMapper, ClearingMerchant> {

	public ResponseData create(ClearingMerchantFrom clearingMerchant) {
		ClearingMerchant entity = transformationFrom(clearingMerchant);
		this.save(entity);
		return ResponseData.success("新增成功");
	}
	
	public ResponseData modify(ClearingMerchantFrom clearingMerchant) {
		if(findById(clearingMerchant.getId())== null) {
			return ResponseData.fail("没有找到对应记录");
		}
		ClearingMerchant entity = transformationFrom(clearingMerchant);
		entity.setId(clearingMerchant.getId());
		this.updateById(entity);
		return ResponseData.success("修改成功");
	}

	public ClearingMerchant transformationFrom(ClearingMerchantFrom clearingMerchant) {
		ClearingMerchant entity = new ClearingMerchant();
		entity.setMerchantCode(clearingMerchant.getMerchantCode());
		entity.setMerchantName(clearingMerchant.getMerchantName());
		entity.setCreateTime(clearingMerchant.getCreateTime());
		entity.setModifyTime(clearingMerchant.getModifyTime());
		entity.setAddress(clearingMerchant.getAddress());
		entity.setPhone(clearingMerchant.getPhone());
		entity.setAlipayAccount(clearingMerchant.getAlipayAccount());
		entity.setClearingType(clearingMerchant.getClearingType());
		entity.setType(clearingMerchant.getType());
		entity.setReserveType(clearingMerchant.getReserveType());
		entity.setDepartmentId(clearingMerchant.getDepartmentId());
		return entity;
	}

	public ClearingMerchantFrom transformationBean(ClearingMerchant clearingMerchant) {
		ClearingMerchantFrom from = new ClearingMerchantFrom();
		from.setMerchantCode(clearingMerchant.getMerchantCode());
		from.setMerchantName(clearingMerchant.getMerchantName());
		from.setCreateTime(clearingMerchant.getCreateTime());
		from.setModifyTime(clearingMerchant.getModifyTime());
		from.setAddress(clearingMerchant.getAddress());
		from.setPhone(clearingMerchant.getPhone());
		from.setAlipayAccount(clearingMerchant.getAlipayAccount());
		from.setClearingType(clearingMerchant.getClearingType());
		from.setType(clearingMerchant.getType());
		from.setReserveType(clearingMerchant.getReserveType());
		from.setDepartmentId(clearingMerchant.getDepartmentId());

		return from;
	}

	
	public ClearingMerchantFrom findById(String id) {
		ClearingMerchant entity = this.getOne(new QueryWrapper<ClearingMerchant>().lambda()
				.eq(ClearingMerchant::getId, id)
				);
		if(entity == null) return null;
		return transformationBean(entity);
	}
	
	public ResponseData remove(String id) {
		if(findById(id)== null) {
			return ResponseData.fail("没有找到对应记录");
		}
		ClearingMerchant clearingMerchant = new ClearingMerchant();
		clearingMerchant.setId(id);
		this.updateById(clearingMerchant);
		return ResponseData.success("删除成功");
	}
	
	public IPage pageList(Integer page, Integer size, ClearingMerchantFrom ibean) {
		return this.page(new Page(page, size), new QueryWrapper<ClearingMerchant>().lambda()
				.eq(StringUtils.isNotEmpty(ibean.getMerchantCode()), ClearingMerchant::getMerchantCode, ibean.getMerchantCode())
				.eq(StringUtils.isNotEmpty(ibean.getMerchantName()), ClearingMerchant::getMerchantName, ibean.getMerchantName())
				.eq(ibean.getCreateTime() != null, ClearingMerchant::getCreateTime, ibean.getCreateTime())
				.eq(ibean.getModifyTime() != null, ClearingMerchant::getModifyTime, ibean.getModifyTime())
				.eq(StringUtils.isNotEmpty(ibean.getAddress()), ClearingMerchant::getAddress, ibean.getAddress())
				.eq(StringUtils.isNotEmpty(ibean.getPhone()), ClearingMerchant::getPhone, ibean.getPhone())
				.eq(StringUtils.isNotEmpty(ibean.getAlipayAccount()), ClearingMerchant::getAlipayAccount, ibean.getAlipayAccount())
				.eq(ibean.getClearingType() != null, ClearingMerchant::getClearingType, ibean.getClearingType())
				.eq(ibean.getType() != null, ClearingMerchant::getType, ibean.getType())
				.eq(ibean.getReserveType() != null, ClearingMerchant::getReserveType, ibean.getReserveType())
				.eq(ibean.getDepartmentId() != null, ClearingMerchant::getDepartmentId, ibean.getDepartmentId())
				);
	}

	@DataSource(value = DataSourceType.ORDER)
	public List<ClearingMerchant> selectList(){
		return list();
	}
}
